﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Threading;
using log4net;
using System.Configuration;

namespace TwKarma.Windows.Service
{
	public partial class KarmaService : ServiceBase
	{
        private int _secondsDelay = int.Parse(ConfigurationManager.AppSettings["RefreshIntervalSeconds"]);

        private ILog _logger;

		public KarmaService()
		{
			InitializeComponent();
		}

		protected override void OnStart(string[] args)
		{
            _logger = LogManager.GetLogger(typeof(KarmaService));
            _logger.Debug("OnStart - enter");

            var worker = new Thread(ProcessKarma);
            worker.Name = "KarmaService";
			worker.IsBackground = false;
			worker.Start();

            _logger.Debug("OnStart - exit");
		}

		protected override void OnStop()
		{
            _logger.Debug("OnStop");
		}

		public void ProcessKarma()
		{
            try
            {
                while (true)
                {
                    _logger.Debug("Call ProcessService.ProcessClient");

                    var client = new Process.ProcessSoapClient();
                    string report = Environment.NewLine +  client.UpdateKarma();
                    _logger.Debug(report);
        
                    System.Threading.Thread.Sleep(_secondsDelay * 1000);
                }
            }
            catch (Exception ex)
            {
                _logger.Fatal("Exception", ex);
            }
		}
	}	
}
